package document;

public class ManagingCommonFile {

	public static final int RECORD_SIZE = 90;
	public static final int BLOCK_SIZE = 90 * 8;
	public static final int FAT_SIZE = 4 * 128;

	
	private Disk disk = new Disk();	
	public void createACommonFile(String name, int size) {

		String[] names = name.split("/");
		int blockNumber = 255;
		
		// 寻找是否有目录
		blockNumber = searchDir(name);

		// 得到最后一个目录的的盘块号
		if (blockNumber != 255) {
			// 遍历该盘块，看是否存在同名文件记录
			// i++;
			int blockNumberOfRecord;
			blockNumberOfRecord = disk.searchARecordOfFile(blockNumber,
					names[names.length - 1]);
			// 找不到该文件名，可以增加记录
			if (blockNumberOfRecord == 255) {
				// 在该盘写入一条文件记录
				// 首先，找到一条空记录

				int dir = 8;
				dir = disk.findANullRecord(blockNumber);
				int index;
				
				if (dir != 8) {

					// 通过FAT寻找是否有足够的空闲区存储文件
					int[] nullBlocks = new int[size + 1];
					ManagingFAT mf = new ManagingFAT();
					nullBlocks = mf.findTheNullBlocks(size);

					// 如果有足够空间，更新fat表
					if (nullBlocks[0] == size) {
						// 读出fat
						int[] fat = new int[128];
						fat = disk.inputDiskToFat();
						// 更新
						for (int j = 1; j < size; j++) {
							fat[nullBlocks[j]] = nullBlocks[j + 1];
						}

						// 末尾置为255
						fat[nullBlocks[size]] = 255;
						// 写回到新的RecordAllocationTable中
						FileAllocationTable newFat = new FileAllocationTable();
						newFat.setFat(fat);
						disk.inputFatToDisk(newFat);

						// 在该记录块增加一条记录
						Record newRecord = new Record(names[names.length - 1],
								'e', nullBlocks[1], size);
						index = FAT_SIZE + BLOCK_SIZE * (blockNumber - 2) + dir
								* RECORD_SIZE;
						disk.writeADirRecoredToDisk(newRecord, index);
						System.out.println("创建文件成功！");

					} else {
						System.out.println("磁盘空间已用完！");
					}

				} else {
					System.out.println("该目录（文件夹）记录已满");
				}

			} else {
				System.out.println("该文件已存在，不能重复创建！");
			}

		} else {
			System.out.println("没有该路径！");

		}

	}

	public void deleteAFile(String name ) {
		 
		String[] names = name.split("/");
		int blockNumber = 255;
		
		// 寻找是否有目录
		blockNumber = searchDir(name);
//
		// 得到最后一个目录的的盘块号
		if (blockNumber != 255) {
			// 遍历该盘块，删除同名文件记录
			int blockNumberOfRecord=disk.searchARecordOfFile(blockNumber,names[names.length - 1]);
			if (blockNumberOfRecord != 255) {
				// 在该盘删除这条文件记录，更新fat
				 disk.resetFileRecordFromABlock(blockNumber, names[names.length-1]);
				ManagingFAT mf=new ManagingFAT();
				mf.ResetTheBlocks(blockNumberOfRecord);
				 System.out.println("文件删除成功");
			}
		else{
			System.out.println("找不到该文件！");
		}
			
	}}

	// 要增加的那条记录的路径是否找得到
	public int searchDir(String name) {

		String[] names = name.split("/");
		int blockNumber = 2;
		int i = 0;
		Disk disk = new Disk();
		if (names[i].equals("Root")) {
			while (blockNumber != 225 && i < names.length - 2){
				i++;
				blockNumber = disk.searchARecordOfDir(blockNumber, names[i]);

				if (blockNumber == 255)
					System.out.print("目录（文件夹）" + "“" + names[i] + "”" + "不存在");

			} 

		} else
			System.out.print("目录（文件夹）" + "“" + names[i] + "”" + "不存在");

		return blockNumber;

	}
	
	
	public Record seeAFile(String name ) {
		 
		String[] names = name.split("/");
		int blockNumber = 255;
		Record record=null;
		// 寻找是否有目录
		blockNumber = searchDir(name);
 
		// 得到最后一个目录的的盘块号
		if (blockNumber != 255) {
		 
			 
				record= disk.seeRecordFromABlock(blockNumber , names[names.length - 1]);
			}
		else{
			System.out.println("找不到该文件！");
		}
		return record;
			
	}}
	

	 

 
